require('../../less/app.less');
//模板配置
var tplArr = [];
var template = require('templateConfig');
var modalTpl =require('raw!../template/modal.txt');
var moduleTpl =require('raw!../template/module.txt');
tplArr['modal'] = template.compile(modalTpl.replace(/^\s*|\s*$/g, ""));
tplArr['module'] = template.compile(moduleTpl.replace(/^\s*|\s*$/g, ""));

function Main(){
    _this = this;
    this.main=function(){
        this.event(this,'tap','m-click');
        this.init();
    }
    //事件封装
    this.event = function(_this,type,name){
        $(document).on(type,'['+name+']',function(e){
          //var ths = $(this)[0];
          var event = $($(this)[0]).attr(name);
          var Fun= event.split(',');
          _this[Fun[0]]($($(this)[0]),Fun[1],e);
        });  
    }
    //区分全半角判断文字长度
    this.getByteLen = function(val){
      if(!val){val=0}
      var len = 0;
      for (var i = 0; i < val.length; i++) {
        if (val.charAt(i).match(/[^\x00-\xff]/ig) != null) //全角
          len += 2; //如果是全角，占用两个字节
        else
          len += 1; //半角占用一个字节
      }
      return len/2;
    }
    //获取url属性值
    this.GetQueryString = function(name){
         var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var url = decodeURI(window.location.search);
         var r = url.substr(1).match(reg);
         if(r!=null)return  unescape(r[2]); return null;
    }
    //公共get方法
    this.get = function(url,data,Fun){
      data.openid = openid;
      $.get(url,data,function(ret){
        Fun(ret);
      },'json');
    }
    //公共post方法
    this.post = function(url,data,Fun,showLoad){ 
      var _this = this;
      //data.openid_test= "ojtKJjq93sF3dOoz4lA12MrxtcUk";
      data.openid = openid;
      console.info(openid);
      if(showLoad){this.loading()}
      $.post(url,data,function(ret){
        Fun(ret);
        _this.dialogClose();
      },'json');
    }
    //提示弹出框
    this.alert = function(options){
      if(!options){var options = {}}
      var data = {
        "Ttype":"alert",
        "title": options.title || "标题",
        "closeFun": options.loseFun || 'm-click="dialogClose"',
        "content": options.content || "内容",
      }
      var alert = $(tplArr['modal'](data)).dialog("show");
      $('body').append(alert);
    }
    //确认弹出框
    this.confirm = function(options){
      if(!options){var options = {}}
      var data = {
        "Ttype":"confirm",
        "title": options.title || "标题",
        "eventFun": options.Fun || 'm-click="dialogClose"',
        "closeFun": options.closeFun || 'm-click="dialogClose"',
        "content": options.content || "内容",
      }
      var alert = $(tplArr['modal'](data)).dialog("show");
      $('body').append(alert);
    }
    //底部弹出框
    this.botActionsheet = function(options){
      if(!options){var options = {}}
      var data = {
        "Ttype":"botActionsheet",
        // "title": options.title || "标题",
        // "eventFun": options.Fun || 'm-click="dialogClose"',
        "closeFun": options.closeFun || 'm-click="dialogClose"',
        "content": options.content || "内容",
      }
      var alert = $(tplArr['modal'](data)).dialog("show");
      $('body').append(alert);
    }
    //关闭提示弹出层
    this.dialogClose = function(){
      $('.ui-dialog,.ui-actionsheet,.ui-loading-block').remove();
    }
    //加载提示
    this.loading = function(text){
      if(!text){var text = "提交中..."}
      $('body').append(tplArr['module']({"tType":"loadingBlock","text":text}));
    }
    this.getHeader= function(){
      var data= { 
        "tType": "header",
        "number": "",  
      };
      var dataC= {
        "tType": "container"
      }
      $(".wrap").html(tplArr['module'](data)); 
      $(".wrap").append(tplArr['module'](dataC)); 
    }

    this.init = function(){
      this.getHeader();
       $(function(){
          orientLayer();
       });
    }
    return this.main();
};
//横屏提示
function orientLayer(){
  var isOchange = false;
  window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
      if (window.orientation === 180 || window.orientation === 0) { 
          isOchange = false;
      } 
      if (window.orientation === 90 || window.orientation === -90 ){ 
          isOchange = true;
      }  
  }, false); 
  $(window).resize(function() {setTimeout(function(){wFun()},0);});
  function wFun(){
    //var Win = $(window);
    var orientLayer = $('#orientLayer');
    // var w = Win.width();
    // var h = Win.height();
    if(isOchange){orientLayer.show()}else{orientLayer.hide()}
  }
  var style = '<style type="text/css">@-webkit-keyframes rotation{10%{transform:rotate(90deg);-webkit-transform:rotate(90deg)}50%,60%{transform:rotate(0deg);-webkit-transform:rotate(0deg)}90%{transform:rotate(90deg);-webkit-transform:rotate(90deg)}100%{transform:rotate(90deg);-webkit-transform:rotate(90deg)}}@keyframes rotation{10%{transform:rotate(90deg);-webkit-transform:rotate(90deg)}50%,60%{transform:rotate(0deg);-webkit-transform:rotate(0deg)}90%{transform:rotate(90deg);-webkit-transform:rotate(90deg)}100%{transform:rotate(90deg);-webkit-transform:rotate(90deg)}}#orientLayer{display:none}@media screen and(min-aspect-ratio:13/8){#orientLayer{display:block}}.mod-orient-layer{display:none;position:fixed;height:100%;width:100%;left:0;top:0;right:0;bottom:0;background:#333;z-index:9997}.mod-orient-layer__content{position:absolute;width:100%;top:45%;margin-top:-75px;text-align:center}.mod-orient-layer__icon-orient{background-image:url("");display:inline-block;width:67px;height:109px;transform:rotate(90deg);-webkit-transform:rotate(90deg);-webkit-animation:rotation infinite 1.5s ease-in-out;animation:rotation infinite 1.5s ease-in-out;-webkit-background-size:67px;background-size:67px}.mod-orient-layer__desc{margin-top:20px;font-size:15px;color:#fff}.qr-box{display:none}@media only screen and(min-width:1023px){html{width:414px;margin:0 auto;background-color:#333!important;position:absolute;left:50%;margin-left:-207px;max-height:672px}.qr-box{position:absolute;left:50%;margin-left:-75px;border-radius:4px;z-index:9999;background-color:#fff;top:50%;margin-top:-100px;display:block}.qr-box__thumb{position:absolute;right:59px;width:25px;height:25px;display:block;top:59px;z-index:9999;padding:2px;-webkit-background-size:25px;background-size:25px;background-repeat:no-repeat;background-position:2px 2px;border:none}.qr-box__thumb_default{background-color:#fff}.qr-box__text{width:100%;text-align:center;font-size:14px;line-height:18px;padding-bottom:10px}.qr-box__img{width:150px;height:150px;border:none;border-radius:10px 10px 0 0;background-size:140px;-webkit-background-size:140px;background-repeat:no-repeat;background-position:5px}.qr-box__img img{padding:5px;width:140px;height:140px}.screen__inner{-webkit-transform:scale(1.29375)!important;transform:scale(1.29375)!important}.mod-orient-layer__content{display:none}}</style>';
  var con = '<div id="orientLayer" class="mod-orient-layer"><div class="mod-orient-layer__content"><i class="icon mod-orient-layer__icon-orient"></i><div class="mod-orient-layer__desc">为了更好的体验，请使用竖屏浏览</div></div></div>';
  $('body').append(style+con);
  wFun();
}

//exports.orientLayer = orientLayer;

var main = new Main();
module.exports = main;